{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e79d817",
   "metadata": {},
   "source": [
    "## 本来也不是写pca+svm，而是直接用pca来做fc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "440b8caa",
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import time\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from sklearn.model_selection import RandomizedSearchCV\n",
    "\n",
    "# Load the Labeled Faces in the Wild (LFW) people dataset (classification).\n",
    "from sklearn.datasets import fetch_lfw_people\n",
    "\n",
    "# Build a text report showing the main classification metrics.\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import ConfusionMatrixDisplay\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.utils.fixes import loguniform\n",
    "\n",
    "\n",
    "# 本来想用thundersvm来调用gpu加速svm运算，结果它太久没更新了，用的libcusparse.so.9.0是\n",
    "# cuda toolkit9.0版本的。但是cuda toolkit9.0只支持version17.的ubuntu，故放弃\n",
    "# libcusparse.so.9.0: cannot open shared object file: No such file or directory\n",
    "# from thundersvm import SVC\n",
    "# import torch\n",
    "\n",
    "# from cuml.svm import SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d5e24db8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Totao dataset size:\n",
      "n_samples: 1288\n",
      "n_features: 1850\n",
      "n_classes: 7\n"
     ]
    }
   ],
   "source": [
    "lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)\n",
    "# lfw_people = fetch_lfw_people( resize=0.4)\n",
    "# introspect the images arrays to find the shapes (for plotting)\n",
    "n_samples, h, w = lfw_people.images.shape\n",
    "\n",
    "# for machine learning we use the 2 data directly (as relative pixel\n",
    "# positions info is ignored by this model)\n",
    "X = lfw_people.data\n",
    "n_features = X.shape[1]\n",
    "\n",
    "# the label to predict is the id of the person\n",
    "y = lfw_people.target\n",
    "target_names = lfw_people.target_names\n",
    "n_classes = target_names.shape[0]\n",
    "\n",
    "print('Totao dataset size:')\n",
    "print('n_samples: %d' % n_samples)\n",
    "print('n_features: %d' % n_features)\n",
    "print('n_classes: %d' % n_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0b0ce71c",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e49b24c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 10 eigenfaces from 966 faces\n",
      "done in 1.059s\n",
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.027s\n"
     ]
    }
   ],
   "source": [
    "n_components = 10\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))\n",
    "\n",
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "8c1fcb21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.8046575 , -1.1224006 ,  0.5765421 , ...,  0.7359646 ,\n",
       "         0.7280152 , -0.27218178],\n",
       "       [ 1.2806776 ,  0.34587222, -0.58530235, ...,  0.03709232,\n",
       "        -0.2683511 , -0.22756146],\n",
       "       [-0.81613797, -0.31701413, -0.06277134, ..., -0.08735423,\n",
       "         1.0495788 , -0.5930591 ],\n",
       "       ...,\n",
       "       [-0.86166745,  0.22000171, -0.90626043, ..., -0.28477457,\n",
       "        -0.5041779 , -1.0351627 ],\n",
       "       [-0.40902898,  0.88871324, -0.97008306, ...,  1.1495458 ,\n",
       "         0.6138435 , -1.9482161 ],\n",
       "       [-1.3751547 , -0.80412185, -0.8978615 , ..., -0.13779353,\n",
       "        -0.37403637, -1.0635834 ]], dtype=float32)"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train_pca"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "c7c7e1ee",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 26 is out of bounds for axis 1 with size 10",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [72]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mX_train_pca\u001b[49m\u001b[43m[\u001b[49m\u001b[43m:\u001b[49m\u001b[43m,\u001b[49m\u001b[43mtemp_imgs_index_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mIndexError\u001b[0m: index 26 is out of bounds for axis 1 with size 10"
     ]
    }
   ],
   "source": [
    "X_train_pca[:,temp_imgs_index_list[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "7fb6c29e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[26,\n",
       "  33,\n",
       "  41,\n",
       "  55,\n",
       "  64,\n",
       "  67,\n",
       "  83,\n",
       "  99,\n",
       "  132,\n",
       "  156,\n",
       "  176,\n",
       "  189,\n",
       "  190,\n",
       "  195,\n",
       "  214,\n",
       "  220,\n",
       "  260,\n",
       "  261,\n",
       "  263,\n",
       "  273,\n",
       "  285,\n",
       "  286,\n",
       "  289,\n",
       "  292,\n",
       "  304,\n",
       "  316,\n",
       "  329,\n",
       "  397,\n",
       "  399,\n",
       "  411,\n",
       "  413,\n",
       "  423,\n",
       "  462,\n",
       "  469,\n",
       "  485,\n",
       "  487,\n",
       "  529,\n",
       "  530,\n",
       "  546,\n",
       "  574,\n",
       "  575,\n",
       "  576,\n",
       "  607,\n",
       "  608,\n",
       "  609,\n",
       "  616,\n",
       "  618,\n",
       "  631,\n",
       "  651,\n",
       "  656,\n",
       "  662,\n",
       "  677,\n",
       "  710,\n",
       "  724,\n",
       "  729,\n",
       "  743,\n",
       "  752,\n",
       "  763,\n",
       "  799,\n",
       "  825,\n",
       "  839,\n",
       "  844,\n",
       "  845,\n",
       "  873],\n",
       " [1,\n",
       "  13,\n",
       "  16,\n",
       "  24,\n",
       "  27,\n",
       "  35,\n",
       "  42,\n",
       "  43,\n",
       "  56,\n",
       "  71,\n",
       "  76,\n",
       "  82,\n",
       "  85,\n",
       "  87,\n",
       "  92,\n",
       "  94,\n",
       "  97,\n",
       "  100,\n",
       "  101,\n",
       "  104,\n",
       "  113,\n",
       "  123,\n",
       "  128,\n",
       "  130,\n",
       "  135,\n",
       "  146,\n",
       "  159,\n",
       "  162,\n",
       "  164,\n",
       "  172,\n",
       "  177,\n",
       "  187,\n",
       "  203,\n",
       "  221,\n",
       "  223,\n",
       "  224,\n",
       "  229,\n",
       "  233,\n",
       "  236,\n",
       "  239,\n",
       "  240,\n",
       "  241,\n",
       "  265,\n",
       "  266,\n",
       "  288,\n",
       "  291,\n",
       "  294,\n",
       "  301,\n",
       "  303,\n",
       "  310,\n",
       "  314,\n",
       "  324,\n",
       "  325,\n",
       "  326,\n",
       "  330,\n",
       "  333,\n",
       "  341,\n",
       "  348,\n",
       "  363,\n",
       "  364,\n",
       "  365,\n",
       "  379,\n",
       "  387,\n",
       "  391,\n",
       "  393,\n",
       "  395,\n",
       "  401,\n",
       "  407,\n",
       "  412,\n",
       "  416,\n",
       "  425,\n",
       "  432,\n",
       "  434,\n",
       "  447,\n",
       "  450,\n",
       "  460,\n",
       "  464,\n",
       "  466,\n",
       "  467,\n",
       "  475,\n",
       "  489,\n",
       "  492,\n",
       "  494,\n",
       "  498,\n",
       "  503,\n",
       "  518,\n",
       "  519,\n",
       "  524,\n",
       "  535,\n",
       "  547,\n",
       "  549,\n",
       "  555,\n",
       "  556,\n",
       "  564,\n",
       "  571,\n",
       "  583,\n",
       "  587,\n",
       "  591,\n",
       "  597,\n",
       "  605,\n",
       "  615,\n",
       "  620,\n",
       "  622,\n",
       "  633,\n",
       "  635,\n",
       "  636,\n",
       "  637,\n",
       "  638,\n",
       "  641,\n",
       "  648,\n",
       "  669,\n",
       "  670,\n",
       "  672,\n",
       "  676,\n",
       "  685,\n",
       "  687,\n",
       "  688,\n",
       "  694,\n",
       "  698,\n",
       "  700,\n",
       "  703,\n",
       "  706,\n",
       "  707,\n",
       "  711,\n",
       "  714,\n",
       "  727,\n",
       "  738,\n",
       "  741,\n",
       "  746,\n",
       "  748,\n",
       "  750,\n",
       "  753,\n",
       "  761,\n",
       "  767,\n",
       "  769,\n",
       "  772,\n",
       "  775,\n",
       "  785,\n",
       "  787,\n",
       "  790,\n",
       "  817,\n",
       "  821,\n",
       "  827,\n",
       "  829,\n",
       "  830,\n",
       "  834,\n",
       "  838,\n",
       "  840,\n",
       "  841,\n",
       "  847,\n",
       "  852,\n",
       "  853,\n",
       "  854,\n",
       "  855,\n",
       "  856,\n",
       "  860,\n",
       "  862,\n",
       "  867,\n",
       "  869,\n",
       "  899,\n",
       "  905,\n",
       "  907,\n",
       "  915,\n",
       "  919,\n",
       "  920,\n",
       "  921,\n",
       "  924,\n",
       "  929,\n",
       "  932,\n",
       "  946,\n",
       "  950,\n",
       "  953,\n",
       "  954,\n",
       "  956,\n",
       "  961,\n",
       "  965],\n",
       " [2,\n",
       "  6,\n",
       "  10,\n",
       "  11,\n",
       "  15,\n",
       "  18,\n",
       "  39,\n",
       "  68,\n",
       "  72,\n",
       "  79,\n",
       "  80,\n",
       "  107,\n",
       "  115,\n",
       "  117,\n",
       "  149,\n",
       "  167,\n",
       "  168,\n",
       "  173,\n",
       "  175,\n",
       "  197,\n",
       "  206,\n",
       "  225,\n",
       "  227,\n",
       "  232,\n",
       "  235,\n",
       "  244,\n",
       "  253,\n",
       "  274,\n",
       "  277,\n",
       "  281,\n",
       "  283,\n",
       "  300,\n",
       "  306,\n",
       "  307,\n",
       "  309,\n",
       "  313,\n",
       "  328,\n",
       "  349,\n",
       "  368,\n",
       "  380,\n",
       "  382,\n",
       "  385,\n",
       "  404,\n",
       "  441,\n",
       "  442,\n",
       "  444,\n",
       "  446,\n",
       "  451,\n",
       "  470,\n",
       "  483,\n",
       "  491,\n",
       "  497,\n",
       "  499,\n",
       "  502,\n",
       "  504,\n",
       "  509,\n",
       "  511,\n",
       "  550,\n",
       "  569,\n",
       "  570,\n",
       "  572,\n",
       "  573,\n",
       "  585,\n",
       "  588,\n",
       "  590,\n",
       "  594,\n",
       "  596,\n",
       "  599,\n",
       "  606,\n",
       "  630,\n",
       "  659,\n",
       "  666,\n",
       "  671,\n",
       "  689,\n",
       "  722,\n",
       "  735,\n",
       "  737,\n",
       "  758,\n",
       "  774,\n",
       "  798,\n",
       "  803,\n",
       "  836,\n",
       "  851,\n",
       "  882,\n",
       "  884,\n",
       "  885,\n",
       "  889,\n",
       "  890,\n",
       "  892,\n",
       "  906,\n",
       "  937,\n",
       "  941,\n",
       "  959,\n",
       "  964],\n",
       " [0,\n",
       "  17,\n",
       "  19,\n",
       "  21,\n",
       "  22,\n",
       "  25,\n",
       "  32,\n",
       "  34,\n",
       "  37,\n",
       "  40,\n",
       "  44,\n",
       "  46,\n",
       "  47,\n",
       "  48,\n",
       "  49,\n",
       "  53,\n",
       "  54,\n",
       "  57,\n",
       "  60,\n",
       "  61,\n",
       "  65,\n",
       "  70,\n",
       "  74,\n",
       "  75,\n",
       "  77,\n",
       "  81,\n",
       "  84,\n",
       "  90,\n",
       "  91,\n",
       "  95,\n",
       "  96,\n",
       "  98,\n",
       "  103,\n",
       "  105,\n",
       "  106,\n",
       "  108,\n",
       "  109,\n",
       "  110,\n",
       "  111,\n",
       "  114,\n",
       "  118,\n",
       "  119,\n",
       "  120,\n",
       "  125,\n",
       "  126,\n",
       "  127,\n",
       "  131,\n",
       "  133,\n",
       "  134,\n",
       "  136,\n",
       "  138,\n",
       "  139,\n",
       "  141,\n",
       "  142,\n",
       "  143,\n",
       "  144,\n",
       "  145,\n",
       "  151,\n",
       "  152,\n",
       "  155,\n",
       "  157,\n",
       "  158,\n",
       "  163,\n",
       "  166,\n",
       "  170,\n",
       "  171,\n",
       "  178,\n",
       "  180,\n",
       "  181,\n",
       "  182,\n",
       "  183,\n",
       "  184,\n",
       "  185,\n",
       "  188,\n",
       "  192,\n",
       "  196,\n",
       "  198,\n",
       "  199,\n",
       "  200,\n",
       "  202,\n",
       "  204,\n",
       "  207,\n",
       "  209,\n",
       "  210,\n",
       "  212,\n",
       "  213,\n",
       "  215,\n",
       "  216,\n",
       "  217,\n",
       "  218,\n",
       "  222,\n",
       "  226,\n",
       "  228,\n",
       "  230,\n",
       "  231,\n",
       "  237,\n",
       "  238,\n",
       "  246,\n",
       "  247,\n",
       "  248,\n",
       "  249,\n",
       "  251,\n",
       "  254,\n",
       "  255,\n",
       "  257,\n",
       "  258,\n",
       "  259,\n",
       "  262,\n",
       "  270,\n",
       "  271,\n",
       "  272,\n",
       "  275,\n",
       "  276,\n",
       "  278,\n",
       "  280,\n",
       "  282,\n",
       "  284,\n",
       "  290,\n",
       "  295,\n",
       "  298,\n",
       "  299,\n",
       "  302,\n",
       "  305,\n",
       "  308,\n",
       "  312,\n",
       "  315,\n",
       "  317,\n",
       "  318,\n",
       "  320,\n",
       "  321,\n",
       "  322,\n",
       "  323,\n",
       "  327,\n",
       "  331,\n",
       "  332,\n",
       "  335,\n",
       "  336,\n",
       "  338,\n",
       "  339,\n",
       "  340,\n",
       "  344,\n",
       "  346,\n",
       "  347,\n",
       "  352,\n",
       "  354,\n",
       "  355,\n",
       "  356,\n",
       "  357,\n",
       "  358,\n",
       "  361,\n",
       "  362,\n",
       "  366,\n",
       "  367,\n",
       "  369,\n",
       "  370,\n",
       "  371,\n",
       "  373,\n",
       "  374,\n",
       "  375,\n",
       "  376,\n",
       "  377,\n",
       "  378,\n",
       "  381,\n",
       "  383,\n",
       "  384,\n",
       "  392,\n",
       "  394,\n",
       "  396,\n",
       "  398,\n",
       "  400,\n",
       "  405,\n",
       "  409,\n",
       "  410,\n",
       "  417,\n",
       "  426,\n",
       "  428,\n",
       "  429,\n",
       "  433,\n",
       "  435,\n",
       "  437,\n",
       "  438,\n",
       "  439,\n",
       "  445,\n",
       "  452,\n",
       "  455,\n",
       "  456,\n",
       "  458,\n",
       "  463,\n",
       "  465,\n",
       "  471,\n",
       "  472,\n",
       "  473,\n",
       "  474,\n",
       "  478,\n",
       "  479,\n",
       "  480,\n",
       "  481,\n",
       "  486,\n",
       "  501,\n",
       "  506,\n",
       "  507,\n",
       "  508,\n",
       "  513,\n",
       "  515,\n",
       "  516,\n",
       "  517,\n",
       "  520,\n",
       "  522,\n",
       "  523,\n",
       "  525,\n",
       "  526,\n",
       "  528,\n",
       "  532,\n",
       "  533,\n",
       "  536,\n",
       "  537,\n",
       "  540,\n",
       "  541,\n",
       "  542,\n",
       "  544,\n",
       "  551,\n",
       "  552,\n",
       "  553,\n",
       "  554,\n",
       "  557,\n",
       "  559,\n",
       "  562,\n",
       "  563,\n",
       "  565,\n",
       "  567,\n",
       "  568,\n",
       "  577,\n",
       "  580,\n",
       "  582,\n",
       "  584,\n",
       "  592,\n",
       "  600,\n",
       "  601,\n",
       "  603,\n",
       "  604,\n",
       "  610,\n",
       "  613,\n",
       "  614,\n",
       "  617,\n",
       "  621,\n",
       "  623,\n",
       "  624,\n",
       "  625,\n",
       "  626,\n",
       "  628,\n",
       "  632,\n",
       "  634,\n",
       "  639,\n",
       "  640,\n",
       "  642,\n",
       "  643,\n",
       "  644,\n",
       "  646,\n",
       "  649,\n",
       "  650,\n",
       "  652,\n",
       "  655,\n",
       "  658,\n",
       "  660,\n",
       "  661,\n",
       "  663,\n",
       "  664,\n",
       "  667,\n",
       "  673,\n",
       "  674,\n",
       "  675,\n",
       "  678,\n",
       "  679,\n",
       "  680,\n",
       "  681,\n",
       "  682,\n",
       "  686,\n",
       "  690,\n",
       "  696,\n",
       "  697,\n",
       "  702,\n",
       "  704,\n",
       "  709,\n",
       "  712,\n",
       "  713,\n",
       "  719,\n",
       "  721,\n",
       "  725,\n",
       "  726,\n",
       "  730,\n",
       "  731,\n",
       "  732,\n",
       "  740,\n",
       "  744,\n",
       "  745,\n",
       "  747,\n",
       "  751,\n",
       "  754,\n",
       "  756,\n",
       "  757,\n",
       "  759,\n",
       "  760,\n",
       "  762,\n",
       "  764,\n",
       "  773,\n",
       "  776,\n",
       "  777,\n",
       "  782,\n",
       "  783,\n",
       "  784,\n",
       "  789,\n",
       "  791,\n",
       "  792,\n",
       "  794,\n",
       "  796,\n",
       "  797,\n",
       "  800,\n",
       "  802,\n",
       "  804,\n",
       "  807,\n",
       "  810,\n",
       "  811,\n",
       "  812,\n",
       "  813,\n",
       "  814,\n",
       "  816,\n",
       "  818,\n",
       "  819,\n",
       "  826,\n",
       "  828,\n",
       "  832,\n",
       "  835,\n",
       "  843,\n",
       "  848,\n",
       "  849,\n",
       "  850,\n",
       "  858,\n",
       "  859,\n",
       "  861,\n",
       "  863,\n",
       "  864,\n",
       "  865,\n",
       "  870,\n",
       "  871,\n",
       "  874,\n",
       "  875,\n",
       "  876,\n",
       "  877,\n",
       "  878,\n",
       "  879,\n",
       "  887,\n",
       "  888,\n",
       "  893,\n",
       "  894,\n",
       "  898,\n",
       "  900,\n",
       "  902,\n",
       "  904,\n",
       "  908,\n",
       "  910,\n",
       "  911,\n",
       "  912,\n",
       "  914,\n",
       "  916,\n",
       "  918,\n",
       "  922,\n",
       "  923,\n",
       "  925,\n",
       "  926,\n",
       "  927,\n",
       "  928,\n",
       "  931,\n",
       "  933,\n",
       "  936,\n",
       "  938,\n",
       "  942,\n",
       "  943,\n",
       "  945,\n",
       "  947,\n",
       "  948,\n",
       "  949,\n",
       "  951,\n",
       "  958,\n",
       "  963],\n",
       " [4,\n",
       "  8,\n",
       "  12,\n",
       "  30,\n",
       "  52,\n",
       "  59,\n",
       "  93,\n",
       "  102,\n",
       "  121,\n",
       "  129,\n",
       "  148,\n",
       "  153,\n",
       "  160,\n",
       "  174,\n",
       "  179,\n",
       "  234,\n",
       "  256,\n",
       "  264,\n",
       "  293,\n",
       "  296,\n",
       "  297,\n",
       "  311,\n",
       "  345,\n",
       "  372,\n",
       "  389,\n",
       "  403,\n",
       "  408,\n",
       "  414,\n",
       "  418,\n",
       "  422,\n",
       "  430,\n",
       "  436,\n",
       "  443,\n",
       "  449,\n",
       "  453,\n",
       "  459,\n",
       "  477,\n",
       "  488,\n",
       "  490,\n",
       "  496,\n",
       "  510,\n",
       "  512,\n",
       "  521,\n",
       "  527,\n",
       "  538,\n",
       "  545,\n",
       "  586,\n",
       "  595,\n",
       "  602,\n",
       "  611,\n",
       "  619,\n",
       "  645,\n",
       "  657,\n",
       "  668,\n",
       "  684,\n",
       "  699,\n",
       "  701,\n",
       "  705,\n",
       "  716,\n",
       "  734,\n",
       "  749,\n",
       "  765,\n",
       "  771,\n",
       "  779,\n",
       "  781,\n",
       "  788,\n",
       "  793,\n",
       "  805,\n",
       "  808,\n",
       "  822,\n",
       "  833,\n",
       "  842,\n",
       "  857,\n",
       "  866,\n",
       "  868,\n",
       "  880,\n",
       "  881,\n",
       "  895,\n",
       "  897,\n",
       "  901,\n",
       "  917,\n",
       "  939,\n",
       "  940,\n",
       "  944],\n",
       " [9,\n",
       "  29,\n",
       "  31,\n",
       "  50,\n",
       "  69,\n",
       "  78,\n",
       "  86,\n",
       "  89,\n",
       "  112,\n",
       "  122,\n",
       "  154,\n",
       "  165,\n",
       "  169,\n",
       "  191,\n",
       "  193,\n",
       "  205,\n",
       "  242,\n",
       "  243,\n",
       "  245,\n",
       "  252,\n",
       "  267,\n",
       "  269,\n",
       "  334,\n",
       "  343,\n",
       "  350,\n",
       "  386,\n",
       "  388,\n",
       "  415,\n",
       "  419,\n",
       "  421,\n",
       "  440,\n",
       "  454,\n",
       "  461,\n",
       "  531,\n",
       "  534,\n",
       "  539,\n",
       "  548,\n",
       "  581,\n",
       "  598,\n",
       "  654,\n",
       "  683,\n",
       "  691,\n",
       "  692,\n",
       "  693,\n",
       "  715,\n",
       "  717,\n",
       "  728,\n",
       "  768,\n",
       "  770,\n",
       "  801,\n",
       "  809,\n",
       "  883,\n",
       "  886,\n",
       "  891,\n",
       "  903,\n",
       "  913],\n",
       " [3,\n",
       "  5,\n",
       "  7,\n",
       "  14,\n",
       "  20,\n",
       "  23,\n",
       "  28,\n",
       "  36,\n",
       "  38,\n",
       "  45,\n",
       "  51,\n",
       "  58,\n",
       "  62,\n",
       "  63,\n",
       "  66,\n",
       "  73,\n",
       "  88,\n",
       "  116,\n",
       "  124,\n",
       "  137,\n",
       "  140,\n",
       "  147,\n",
       "  150,\n",
       "  161,\n",
       "  186,\n",
       "  194,\n",
       "  201,\n",
       "  208,\n",
       "  211,\n",
       "  219,\n",
       "  250,\n",
       "  268,\n",
       "  279,\n",
       "  287,\n",
       "  319,\n",
       "  337,\n",
       "  342,\n",
       "  351,\n",
       "  353,\n",
       "  359,\n",
       "  360,\n",
       "  390,\n",
       "  402,\n",
       "  406,\n",
       "  420,\n",
       "  424,\n",
       "  427,\n",
       "  431,\n",
       "  448,\n",
       "  457,\n",
       "  468,\n",
       "  476,\n",
       "  482,\n",
       "  484,\n",
       "  493,\n",
       "  495,\n",
       "  500,\n",
       "  505,\n",
       "  514,\n",
       "  543,\n",
       "  558,\n",
       "  560,\n",
       "  561,\n",
       "  566,\n",
       "  578,\n",
       "  579,\n",
       "  589,\n",
       "  593,\n",
       "  612,\n",
       "  627,\n",
       "  629,\n",
       "  647,\n",
       "  653,\n",
       "  665,\n",
       "  695,\n",
       "  708,\n",
       "  718,\n",
       "  720,\n",
       "  723,\n",
       "  733,\n",
       "  736,\n",
       "  739,\n",
       "  742,\n",
       "  755,\n",
       "  766,\n",
       "  778,\n",
       "  780,\n",
       "  786,\n",
       "  795,\n",
       "  806,\n",
       "  815,\n",
       "  820,\n",
       "  823,\n",
       "  824,\n",
       "  831,\n",
       "  837,\n",
       "  846,\n",
       "  872,\n",
       "  896,\n",
       "  909,\n",
       "  930,\n",
       "  934,\n",
       "  935,\n",
       "  952,\n",
       "  955,\n",
       "  957,\n",
       "  960,\n",
       "  962]]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 找出所有j类别的训练数据\n",
    "temp_imgs_index_list = []\n",
    "for j in range(n_classes):\n",
    "    temp_list = []\n",
    "    for k in range(len(y_train)):\n",
    "        if y_train[k] == j:\n",
    "            temp_list.append(k)\n",
    "    temp_imgs_index_list.append(temp_list)\n",
    "temp_imgs_index_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "90811f2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    for j in range(n_classes):\n",
    "        temp_imgs = X_train_pca[temp_imgs_index_list[j],:]\n",
    "        mean_temp = np.mean(temp_imgs, 1)\n",
    "        diffMat = mean_temp - np.tile(testFace, (len(X_train_pca), 1)) # 计算训练数据与测试数据的距离\n",
    "        sqDiffMat = diffMat**2\n",
    "        sqDistances = sqDiffMat.sum(axis=1)\n",
    "        sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "        indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "        if sorted(sqDistances)[0] < Threshold:\n",
    "            y_pred[i] = y[indexMin]\n",
    "        else:\n",
    "            y_pred[i] = -1 # -1表示unkown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "03afb797",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.00175714,  0.00125861,  0.00884676, ...,  0.0186433 ,\n",
       "         0.01397018,  0.01172301],\n",
       "       [-0.00175714,  0.00125861,  0.00884676, ...,  0.0186433 ,\n",
       "         0.01397018,  0.01172301],\n",
       "       [-0.00175714,  0.00125861,  0.00884676, ...,  0.0186433 ,\n",
       "         0.01397018,  0.01172301],\n",
       "       ...,\n",
       "       [-0.00175714,  0.00125861,  0.00884676, ...,  0.0186433 ,\n",
       "         0.01397018,  0.01172301],\n",
       "       [-0.00175714,  0.00125861,  0.00884676, ...,  0.0186433 ,\n",
       "         0.01397018,  0.01172301],\n",
       "       [-0.00175714,  0.00125861,  0.00884676, ...,  0.0186433 ,\n",
       "         0.01397018,  0.01172301]], dtype=float32)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp_imgs = X_train_pca[temp_imgs_index_list[0],:]\n",
    "mean_temp = np.mean(temp_imgs, 1)\n",
    "diffMat = mean_temp - np.tile(testFace, (len(X_train_pca), 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "90f01acf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 暂时不能动的代码\n",
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sorted(sqDistances)[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "9465108c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.02500366, -0.02161827, -0.02440197, ..., -0.0537263 ,\n",
       "        -0.06450487, -0.0694282 ],\n",
       "       [ 0.03540347,  0.0422095 ,  0.04385747, ..., -0.02372944,\n",
       "        -0.03178684, -0.03655289],\n",
       "       [-0.04133617, -0.03850536, -0.04171024, ..., -0.03930137,\n",
       "        -0.03936403, -0.03615703],\n",
       "       ...,\n",
       "       [ 0.06093669,  0.05108757,  0.0418124 , ...,  0.11038213,\n",
       "         0.11624854,  0.11705916],\n",
       "       [-0.07328448, -0.09623087, -0.11868525, ...,  0.0567244 ,\n",
       "         0.07601508,  0.09012321],\n",
       "       [-0.00951146, -0.0141109 , -0.0305115 , ...,  0.03248752,\n",
       "         0.03936281,  0.0479978 ]], dtype=float32)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "42c3d36f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 6, 3, 3, 3, 4, 1, 3, 3, 3, 3, 3, 6, 3, 3, 3, 3, 3, 4, 1, 2,\n",
       "       3, 2, 0, 1, 2, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3, 1,\n",
       "       1, 1, 4, 3, 2, 3, 3, 3, 0, 3, 6, 2, 1, 3, 5, 3, 1, 1, 1, 4, 3, 5,\n",
       "       6, 4, 1, 3, 5, 6, 3, 3, 3, 2, 1, 6, 4, 4, 3, 0, 4, 3, 3, 3, 3, 3,\n",
       "       3, 3, 3, 6, 3, 4, 1, 5, 1, 1, 6, 6, 3, 1, 3, 1, 3, 3, 3, 3, 3, 1,\n",
       "       4, 1, 3, 3, 3, 1, 3, 4, 1, 3, 1, 3, 3, 0, 3, 4, 4, 3, 1, 1, 6, 6,\n",
       "       6, 6, 2, 4, 3, 3, 1, 6, 2, 2, 5, 1, 3, 5, 1, 3, 6, 1, 5, 1, 1, 3,\n",
       "       3, 3, 6, 0, 1, 3, 6, 5, 5, 1, 3, 5, 5, 1, 0, 3, 1, 1, 6, 1, 5, 6,\n",
       "       3, 2, 2, 4, 3, 3, 3, 2, 3, 3, 3, 3, 3, 2, 3, 2, 3, 2, 6, 3, 3, 6,\n",
       "       3, 6, 3, 2, 1, 2, 3, 3, 6, 2, 1, 0, 3, 5, 3, 3, 3, 3, 3, 0, 0, 1,\n",
       "       3, 3, 1, 1, 6, 3, 3, 3, 1, 3, 3, 3, 1, 0, 3, 1, 6, 3, 3, 3, 3, 4,\n",
       "       2, 4, 3, 0, 3, 3, 3, 6, 4, 3, 2, 6, 3, 4, 2, 1, 6, 2, 2, 3, 6, 1,\n",
       "       3, 4, 3, 1, 4, 6, 1, 1, 3, 3, 6, 3, 6, 3, 3, 3, 1, 2, 3, 3, 1, 0,\n",
       "       3, 3, 3, 4, 4, 3, 5, 1, 3, 0, 4, 1, 3, 4, 3, 6, 6, 2, 1, 3, 1, 3,\n",
       "       1, 3, 3, 3, 1, 6, 3, 3, 6, 1, 3, 3, 5, 2])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "aa479dde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0006242999"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(sqDistances)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e6d3c1d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 6, 3, 1, 5, 1, 1, 3, 3, 4, 6, 6, 1, 3, 4, 1, 3, 3, 6, 3, 3, 3,\n",
       "       3, 5, 3, 3, 4, 3, 6, 0, 3, 1, 1, 3, 4, 3, 0, 2, 6, 1, 1, 3, 2, 3,\n",
       "       3, 3, 1, 6, 1, 6, 1, 0, 1, 3, 2, 1, 1, 3, 6, 0, 3, 4, 0, 5, 2, 4,\n",
       "       3, 3, 3, 3, 5, 1, 3, 6, 6, 3, 4, 3, 3, 4, 1, 2, 4, 1, 5, 3, 3, 2,\n",
       "       1, 3, 3, 3, 3, 6, 3, 2, 3, 3, 1, 2, 3, 1, 6, 2, 3, 1, 1, 4, 3, 1,\n",
       "       6, 4, 3, 3, 5, 4, 1, 3, 3, 3, 3, 3, 2, 0, 3, 6, 0, 4, 3, 3, 3, 3,\n",
       "       3, 6, 1, 3, 2, 0, 1, 3, 3, 3, 6, 2, 3, 3, 3, 3, 1, 3, 3, 1, 2, 3,\n",
       "       2, 6, 6, 1, 2, 0, 3, 1, 0, 1, 3, 3, 3, 3, 6, 2, 1, 3, 1, 3, 1, 3,\n",
       "       3, 3, 1, 2, 4, 1, 0, 4, 2, 3, 1, 0, 3, 3, 2, 3, 3, 3, 3, 6, 3, 0,\n",
       "       6, 3, 2, 0, 3, 3, 3, 4, 6, 1, 6, 1, 1, 6, 1, 3, 6, 0, 6, 1, 3, 3,\n",
       "       3, 1, 3, 1, 3, 1, 1, 3, 0, 3, 0, 5, 6, 3, 5, 3, 3, 1, 2, 3, 1, 3,\n",
       "       6, 1, 3, 3, 4, 3, 1, 3, 1, 2, 6, 5, 1, 1, 3, 4, 1, 0, 6, 3, 4, 3,\n",
       "       3, 3, 3, 6, 6, 1, 3, 6, 2, 2, 0, 1, 1, 0, 6, 1, 3, 1, 0, 0, 3, 6,\n",
       "       1, 3, 1, 3, 3, 2, 4, 6, 3, 6, 6, 4, 2, 3, 1, 4, 3, 1, 3, 2, 3, 0,\n",
       "       2, 3, 1, 3, 3, 1, 2, 2, 0, 3, 3, 2, 3, 6])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "396956ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.04      0.08      0.05        13\n",
      "     Colin Powell       0.14      0.15      0.14        60\n",
      "  Donald Rumsfeld       0.00      0.00      0.00        27\n",
      "    George W Bush       0.38      0.34      0.36       146\n",
      "Gerhard Schroeder       0.09      0.08      0.09        25\n",
      "      Hugo Chavez       0.11      0.07      0.08        15\n",
      "       Tony Blair       0.07      0.08      0.08        36\n",
      "\n",
      "         accuracy                           0.20       322\n",
      "        macro avg       0.12      0.11      0.11       322\n",
      "     weighted avg       0.22      0.20      0.21       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(y_test, np.array(y_pred), target_names=target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e3657024",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Input \u001b[0;32mIn [46]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mConfusionMatrixDisplay\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_predictions\u001b[49m\u001b[43m(\u001b[49m\u001b[43my_test\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43my_pred\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlabels\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdisplay_labels\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mtarget_names\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mxticks_rotation\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mvertical\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m      2\u001b[0m plt\u001b[38;5;241m.\u001b[39mshow()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/sklearn/metrics/_plot/confusion_matrix.py:433\u001b[0m, in \u001b[0;36mConfusionMatrixDisplay.from_predictions\u001b[0;34m(cls, y_true, y_pred, labels, sample_weight, normalize, display_labels, include_values, xticks_rotation, values_format, cmap, ax, colorbar)\u001b[0m\n\u001b[1;32m    423\u001b[0m cm \u001b[38;5;241m=\u001b[39m confusion_matrix(\n\u001b[1;32m    424\u001b[0m     y_true,\n\u001b[1;32m    425\u001b[0m     y_pred,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    428\u001b[0m     normalize\u001b[38;5;241m=\u001b[39mnormalize,\n\u001b[1;32m    429\u001b[0m )\n\u001b[1;32m    431\u001b[0m disp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m(confusion_matrix\u001b[38;5;241m=\u001b[39mcm, display_labels\u001b[38;5;241m=\u001b[39mdisplay_labels)\n\u001b[0;32m--> 433\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdisp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplot\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    434\u001b[0m \u001b[43m    \u001b[49m\u001b[43minclude_values\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude_values\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    435\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcmap\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcmap\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    436\u001b[0m \u001b[43m    \u001b[49m\u001b[43max\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43max\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    437\u001b[0m \u001b[43m    \u001b[49m\u001b[43mxticks_rotation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mxticks_rotation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    438\u001b[0m \u001b[43m    \u001b[49m\u001b[43mvalues_format\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalues_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    439\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcolorbar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolorbar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    440\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/sklearn/metrics/_plot/confusion_matrix.py:142\u001b[0m, in \u001b[0;36mConfusionMatrixDisplay.plot\u001b[0;34m(self, include_values, cmap, xticks_rotation, values_format, ax, colorbar)\u001b[0m\n\u001b[1;32m    139\u001b[0m thresh \u001b[38;5;241m=\u001b[39m (cm\u001b[38;5;241m.\u001b[39mmax() \u001b[38;5;241m+\u001b[39m cm\u001b[38;5;241m.\u001b[39mmin()) \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m2.0\u001b[39m\n\u001b[1;32m    141\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, j \u001b[38;5;129;01min\u001b[39;00m product(\u001b[38;5;28mrange\u001b[39m(n_classes), \u001b[38;5;28mrange\u001b[39m(n_classes)):\n\u001b[0;32m--> 142\u001b[0m     color \u001b[38;5;241m=\u001b[39m cmap_max \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mcm\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mj\u001b[49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m<\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mthresh\u001b[49m \u001b[38;5;28;01melse\u001b[39;00m cmap_min\n\u001b[1;32m    144\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m values_format \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    145\u001b[0m         text_cm \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mformat\u001b[39m(cm[i, j], \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.2g\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error in callback <function flush_figures at 0x7fede1996f70> (for post_execute):\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib_inline/backend_inline.py:121\u001b[0m, in \u001b[0;36mflush_figures\u001b[0;34m()\u001b[0m\n\u001b[1;32m    118\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m InlineBackend\u001b[38;5;241m.\u001b[39minstance()\u001b[38;5;241m.\u001b[39mclose_figures:\n\u001b[1;32m    119\u001b[0m     \u001b[38;5;66;03m# ignore the tracking, just draw and close all figures\u001b[39;00m\n\u001b[1;32m    120\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 121\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mshow\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m    122\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    123\u001b[0m         \u001b[38;5;66;03m# safely show traceback if in IPython, else raise\u001b[39;00m\n\u001b[1;32m    124\u001b[0m         ip \u001b[38;5;241m=\u001b[39m get_ipython()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib_inline/backend_inline.py:41\u001b[0m, in \u001b[0;36mshow\u001b[0;34m(close, block)\u001b[0m\n\u001b[1;32m     39\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m     40\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m figure_manager \u001b[38;5;129;01min\u001b[39;00m Gcf\u001b[38;5;241m.\u001b[39mget_all_fig_managers():\n\u001b[0;32m---> 41\u001b[0m         \u001b[43mdisplay\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     42\u001b[0m \u001b[43m            \u001b[49m\u001b[43mfigure_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanvas\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     43\u001b[0m \u001b[43m            \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_fetch_figure_metadata\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfigure_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanvas\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     44\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     45\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     46\u001b[0m     show\u001b[38;5;241m.\u001b[39m_to_draw \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/display_functions.py:298\u001b[0m, in \u001b[0;36mdisplay\u001b[0;34m(include, exclude, metadata, transient, display_id, raw, clear, *objs, **kwargs)\u001b[0m\n\u001b[1;32m    296\u001b[0m     publish_display_data(data\u001b[38;5;241m=\u001b[39mobj, metadata\u001b[38;5;241m=\u001b[39mmetadata, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m    297\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 298\u001b[0m     format_dict, md_dict \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mformat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minclude\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexclude\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexclude\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    299\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m format_dict:\n\u001b[1;32m    300\u001b[0m         \u001b[38;5;66;03m# nothing to display (e.g. _ipython_display_ took over)\u001b[39;00m\n\u001b[1;32m    301\u001b[0m         \u001b[38;5;28;01mcontinue\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/formatters.py:178\u001b[0m, in \u001b[0;36mDisplayFormatter.format\u001b[0;34m(self, obj, include, exclude)\u001b[0m\n\u001b[1;32m    176\u001b[0m md \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    177\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 178\u001b[0m     data \u001b[38;5;241m=\u001b[39m \u001b[43mformatter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    179\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n\u001b[1;32m    180\u001b[0m     \u001b[38;5;66;03m# FIXME: log the exception\u001b[39;00m\n\u001b[1;32m    181\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/decorator.py:232\u001b[0m, in \u001b[0;36mdecorate.<locals>.fun\u001b[0;34m(*args, **kw)\u001b[0m\n\u001b[1;32m    230\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kwsyntax:\n\u001b[1;32m    231\u001b[0m     args, kw \u001b[38;5;241m=\u001b[39m fix(args, kw, sig)\n\u001b[0;32m--> 232\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcaller\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mextras\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/formatters.py:222\u001b[0m, in \u001b[0;36mcatch_format_error\u001b[0;34m(method, self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    220\u001b[0m \u001b[38;5;124;03m\"\"\"show traceback on failed format call\"\"\"\u001b[39;00m\n\u001b[1;32m    221\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 222\u001b[0m     r \u001b[38;5;241m=\u001b[39m \u001b[43mmethod\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    223\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m:\n\u001b[1;32m    224\u001b[0m     \u001b[38;5;66;03m# don't warn on NotImplementedErrors\u001b[39;00m\n\u001b[1;32m    225\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_return(\u001b[38;5;28;01mNone\u001b[39;00m, args[\u001b[38;5;241m0\u001b[39m])\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/formatters.py:339\u001b[0m, in \u001b[0;36mBaseFormatter.__call__\u001b[0;34m(self, obj)\u001b[0m\n\u001b[1;32m    337\u001b[0m     \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[1;32m    338\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 339\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mprinter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    340\u001b[0m \u001b[38;5;66;03m# Finally look for special method names\u001b[39;00m\n\u001b[1;32m    341\u001b[0m method \u001b[38;5;241m=\u001b[39m get_real_method(obj, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprint_method)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/pylabtools.py:151\u001b[0m, in \u001b[0;36mprint_figure\u001b[0;34m(fig, fmt, bbox_inches, base64, **kwargs)\u001b[0m\n\u001b[1;32m    148\u001b[0m     \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbackend_bases\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FigureCanvasBase\n\u001b[1;32m    149\u001b[0m     FigureCanvasBase(fig)\n\u001b[0;32m--> 151\u001b[0m \u001b[43mfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanvas\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprint_figure\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbytes_io\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    152\u001b[0m data \u001b[38;5;241m=\u001b[39m bytes_io\u001b[38;5;241m.\u001b[39mgetvalue()\n\u001b[1;32m    153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m fmt \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msvg\u001b[39m\u001b[38;5;124m'\u001b[39m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/backend_bases.py:2295\u001b[0m, in \u001b[0;36mFigureCanvasBase.print_figure\u001b[0;34m(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)\u001b[0m\n\u001b[1;32m   2289\u001b[0m     renderer \u001b[38;5;241m=\u001b[39m _get_renderer(\n\u001b[1;32m   2290\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfigure,\n\u001b[1;32m   2291\u001b[0m         functools\u001b[38;5;241m.\u001b[39mpartial(\n\u001b[1;32m   2292\u001b[0m             print_method, orientation\u001b[38;5;241m=\u001b[39morientation)\n\u001b[1;32m   2293\u001b[0m     )\n\u001b[1;32m   2294\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(renderer, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_draw_disabled\u001b[39m\u001b[38;5;124m\"\u001b[39m, nullcontext)():\n\u001b[0;32m-> 2295\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2297\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m bbox_inches:\n\u001b[1;32m   2298\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m bbox_inches \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtight\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:73\u001b[0m, in \u001b[0;36m_finalize_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer, *args, **kwargs)\u001b[0m\n\u001b[1;32m     71\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(draw)\n\u001b[1;32m     72\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdraw_wrapper\u001b[39m(artist, renderer, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m---> 73\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     74\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m renderer\u001b[38;5;241m.\u001b[39m_rasterizing:\n\u001b[1;32m     75\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstop_rasterizing()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:50\u001b[0m, in \u001b[0;36mallow_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer)\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m     48\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstart_filter()\n\u001b[0;32m---> 50\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/figure.py:2810\u001b[0m, in \u001b[0;36mFigure.draw\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m   2807\u001b[0m         \u001b[38;5;66;03m# ValueError can occur when resizing a window.\u001b[39;00m\n\u001b[1;32m   2809\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpatch\u001b[38;5;241m.\u001b[39mdraw(renderer)\n\u001b[0;32m-> 2810\u001b[0m \u001b[43mmimage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_draw_list_compositing_images\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   2811\u001b[0m \u001b[43m    \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43martists\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msuppressComposite\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2813\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m sfig \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msubfigs:\n\u001b[1;32m   2814\u001b[0m     sfig\u001b[38;5;241m.\u001b[39mdraw(renderer)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/image.py:132\u001b[0m, in \u001b[0;36m_draw_list_compositing_images\u001b[0;34m(renderer, parent, artists, suppress_composite)\u001b[0m\n\u001b[1;32m    130\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m not_composite \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m has_images:\n\u001b[1;32m    131\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m a \u001b[38;5;129;01min\u001b[39;00m artists:\n\u001b[0;32m--> 132\u001b[0m         \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    133\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    134\u001b[0m     \u001b[38;5;66;03m# Composite any adjacent images together\u001b[39;00m\n\u001b[1;32m    135\u001b[0m     image_group \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:50\u001b[0m, in \u001b[0;36mallow_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer)\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m     48\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstart_filter()\n\u001b[0;32m---> 50\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/axes/_base.py:3082\u001b[0m, in \u001b[0;36m_AxesBase.draw\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m   3079\u001b[0m         a\u001b[38;5;241m.\u001b[39mdraw(renderer)\n\u001b[1;32m   3080\u001b[0m     renderer\u001b[38;5;241m.\u001b[39mstop_rasterizing()\n\u001b[0;32m-> 3082\u001b[0m \u001b[43mmimage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_draw_list_compositing_images\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   3083\u001b[0m \u001b[43m    \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43martists\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msuppressComposite\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   3085\u001b[0m renderer\u001b[38;5;241m.\u001b[39mclose_group(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxes\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m   3086\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstale \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/image.py:132\u001b[0m, in \u001b[0;36m_draw_list_compositing_images\u001b[0;34m(renderer, parent, artists, suppress_composite)\u001b[0m\n\u001b[1;32m    130\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m not_composite \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m has_images:\n\u001b[1;32m    131\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m a \u001b[38;5;129;01min\u001b[39;00m artists:\n\u001b[0;32m--> 132\u001b[0m         \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    133\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    134\u001b[0m     \u001b[38;5;66;03m# Composite any adjacent images together\u001b[39;00m\n\u001b[1;32m    135\u001b[0m     image_group \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:50\u001b[0m, in \u001b[0;36mallow_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer)\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m     48\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstart_filter()\n\u001b[0;32m---> 50\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/text.py:685\u001b[0m, in \u001b[0;36mText.draw\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m    682\u001b[0m renderer\u001b[38;5;241m.\u001b[39mopen_group(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtext\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_gid())\n\u001b[1;32m    684\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cm_set(text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_wrapped_text()):\n\u001b[0;32m--> 685\u001b[0m     bbox, info, descent \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_layout\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    686\u001b[0m     trans \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_transform()\n\u001b[1;32m    688\u001b[0m     \u001b[38;5;66;03m# don't use self.get_position here, which refers to text\u001b[39;00m\n\u001b[1;32m    689\u001b[0m     \u001b[38;5;66;03m# position in Text:\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/text.py:358\u001b[0m, in \u001b[0;36mText._get_layout\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m    355\u001b[0m height \u001b[38;5;241m=\u001b[39m ymax \u001b[38;5;241m-\u001b[39m ymin\n\u001b[1;32m    357\u001b[0m \u001b[38;5;66;03m# get the rotation matrix\u001b[39;00m\n\u001b[0;32m--> 358\u001b[0m M \u001b[38;5;241m=\u001b[39m \u001b[43mAffine2D\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrotate_deg\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_rotation\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    360\u001b[0m \u001b[38;5;66;03m# now offset the individual text lines within the box\u001b[39;00m\n\u001b[1;32m    361\u001b[0m malign \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_multialignment()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/transforms.py:2020\u001b[0m, in \u001b[0;36mAffine2D.rotate_deg\u001b[0;34m(self, degrees)\u001b[0m\n\u001b[1;32m   2012\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrotate_deg\u001b[39m(\u001b[38;5;28mself\u001b[39m, degrees):\n\u001b[1;32m   2013\u001b[0m     \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m   2014\u001b[0m \u001b[38;5;124;03m    Add a rotation (in degrees) to this transform in place.\u001b[39;00m\n\u001b[1;32m   2015\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   2018\u001b[0m \u001b[38;5;124;03m    and :meth:`scale`.\u001b[39;00m\n\u001b[1;32m   2019\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m-> 2020\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrotate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mradians\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdegrees\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/transforms.py:2008\u001b[0m, in \u001b[0;36mAffine2D.rotate\u001b[0;34m(self, theta)\u001b[0m\n\u001b[1;32m   2005\u001b[0m b \u001b[38;5;241m=\u001b[39m math\u001b[38;5;241m.\u001b[39msin(theta)\n\u001b[1;32m   2006\u001b[0m rotate_mtx \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([[a, \u001b[38;5;241m-\u001b[39mb, \u001b[38;5;241m0.0\u001b[39m], [b, a, \u001b[38;5;241m0.0\u001b[39m], [\u001b[38;5;241m0.0\u001b[39m, \u001b[38;5;241m0.0\u001b[39m, \u001b[38;5;241m1.0\u001b[39m]],\n\u001b[1;32m   2007\u001b[0m                       \u001b[38;5;28mfloat\u001b[39m)\n\u001b[0;32m-> 2008\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_mtx \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrotate_mtx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_mtx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2009\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minvalidate()\n\u001b[1;32m   2010\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\n",
      "File \u001b[0;32m<__array_function__ internals>:5\u001b[0m, in \u001b[0;36mdot\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "ConfusionMatrixDisplay.from_predictions(y_test, np.array(y_pred), labels=y, display_labels = target_names, xticks_rotation = 'vertical')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2503d488",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Input \u001b[0;32mIn [25]\u001b[0m, in \u001b[0;36m<cell line: 4>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m cm \u001b[38;5;241m=\u001b[39m confusion_matrix(y_test, y_pred, labels\u001b[38;5;241m=\u001b[39my)\n\u001b[1;32m      2\u001b[0m disp \u001b[38;5;241m=\u001b[39m ConfusionMatrixDisplay(confusion_matrix\u001b[38;5;241m=\u001b[39mcm,\n\u001b[1;32m      3\u001b[0m                               display_labels\u001b[38;5;241m=\u001b[39mtarget_names)\n\u001b[0;32m----> 4\u001b[0m \u001b[43mdisp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplot\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      6\u001b[0m plt\u001b[38;5;241m.\u001b[39mshow()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/sklearn/metrics/_plot/confusion_matrix.py:153\u001b[0m, in \u001b[0;36mConfusionMatrixDisplay.plot\u001b[0;34m(self, include_values, cmap, xticks_rotation, values_format, ax, colorbar)\u001b[0m\n\u001b[1;32m    150\u001b[0m         \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    151\u001b[0m             text_cm \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mformat\u001b[39m(cm[i, j], values_format)\n\u001b[0;32m--> 153\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtext_[i, j] \u001b[38;5;241m=\u001b[39m \u001b[43max\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtext\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    154\u001b[0m \u001b[43m            \u001b[49m\u001b[43mj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtext_cm\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mha\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcenter\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mva\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcenter\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolor\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolor\u001b[49m\n\u001b[1;32m    155\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    157\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdisplay_labels \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    158\u001b[0m     display_labels \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marange(n_classes)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/axes/_axes.py:660\u001b[0m, in \u001b[0;36mAxes.text\u001b[0;34m(self, x, y, s, fontdict, **kwargs)\u001b[0m\n\u001b[1;32m    651\u001b[0m effective_kwargs \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m    652\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mverticalalignment\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbaseline\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m    653\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhorizontalalignment\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mleft\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    657\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[1;32m    658\u001b[0m }\n\u001b[1;32m    659\u001b[0m t \u001b[38;5;241m=\u001b[39m mtext\u001b[38;5;241m.\u001b[39mText(x, y, text\u001b[38;5;241m=\u001b[39ms, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39meffective_kwargs)\n\u001b[0;32m--> 660\u001b[0m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset_clip_path\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpatch\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    661\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_add_text(t)\n\u001b[1;32m    662\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m t\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/text.py:531\u001b[0m, in \u001b[0;36mText.set_clip_path\u001b[0;34m(self, path, transform)\u001b[0m\n\u001b[1;32m    529\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mset_clip_path\u001b[39m(\u001b[38;5;28mself\u001b[39m, path, transform\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m    530\u001b[0m     \u001b[38;5;66;03m# docstring inherited.\u001b[39;00m\n\u001b[0;32m--> 531\u001b[0m     \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset_clip_path\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpath\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtransform\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    532\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_update_clip_properties()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:790\u001b[0m, in \u001b[0;36mArtist.set_clip_path\u001b[0;34m(self, path, transform)\u001b[0m\n\u001b[1;32m    787\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m transform \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    788\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(path, Rectangle):\n\u001b[1;32m    789\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclipbox \u001b[38;5;241m=\u001b[39m TransformedBbox(Bbox\u001b[38;5;241m.\u001b[39munit(),\n\u001b[0;32m--> 790\u001b[0m                                        \u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_transform\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_clippath \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    792\u001b[0m         success \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/patches.py:278\u001b[0m, in \u001b[0;36mPatch.get_transform\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    276\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_transform\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m    277\u001b[0m     \u001b[38;5;124;03m\"\"\"Return the `~.transforms.Transform` applied to the `Patch`.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 278\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_patch_transform\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m+\u001b[39m artist\u001b[38;5;241m.\u001b[39mArtist\u001b[38;5;241m.\u001b[39mget_transform(\u001b[38;5;28mself\u001b[39m)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/patches.py:754\u001b[0m, in \u001b[0;36mRectangle.get_patch_transform\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    747\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_patch_transform\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m    748\u001b[0m     \u001b[38;5;66;03m# Note: This cannot be called until after this has been added to\u001b[39;00m\n\u001b[1;32m    749\u001b[0m     \u001b[38;5;66;03m# an Axes, otherwise unit conversion will fail. This makes it very\u001b[39;00m\n\u001b[1;32m    750\u001b[0m     \u001b[38;5;66;03m# important to call the accessor method and not directly access the\u001b[39;00m\n\u001b[1;32m    751\u001b[0m     \u001b[38;5;66;03m# transformation member variable.\u001b[39;00m\n\u001b[1;32m    752\u001b[0m     bbox \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_bbox()\n\u001b[1;32m    753\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m (transforms\u001b[38;5;241m.\u001b[39mBboxTransformTo(bbox)\n\u001b[0;32m--> 754\u001b[0m             \u001b[38;5;241m+\u001b[39m \u001b[43mtransforms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mAffine2D\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrotate_deg_around\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    755\u001b[0m \u001b[43m                \u001b[49m\u001b[43mbbox\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mx0\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbbox\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43my0\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mangle\u001b[49m\u001b[43m)\u001b[49m)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/transforms.py:2042\u001b[0m, in \u001b[0;36mAffine2D.rotate_deg_around\u001b[0;34m(self, x, y, degrees)\u001b[0m\n\u001b[1;32m   2040\u001b[0m \u001b[38;5;66;03m# Cast to float to avoid wraparound issues with uint8's\u001b[39;00m\n\u001b[1;32m   2041\u001b[0m x, y \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mfloat\u001b[39m(x), \u001b[38;5;28mfloat\u001b[39m(y)\n\u001b[0;32m-> 2042\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtranslate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43my\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrotate_deg\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdegrees\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mtranslate(x, y)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/transforms.py:2020\u001b[0m, in \u001b[0;36mAffine2D.rotate_deg\u001b[0;34m(self, degrees)\u001b[0m\n\u001b[1;32m   2012\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrotate_deg\u001b[39m(\u001b[38;5;28mself\u001b[39m, degrees):\n\u001b[1;32m   2013\u001b[0m     \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m   2014\u001b[0m \u001b[38;5;124;03m    Add a rotation (in degrees) to this transform in place.\u001b[39;00m\n\u001b[1;32m   2015\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   2018\u001b[0m \u001b[38;5;124;03m    and :meth:`scale`.\u001b[39;00m\n\u001b[1;32m   2019\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m-> 2020\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrotate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mradians\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdegrees\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/transforms.py:2006\u001b[0m, in \u001b[0;36mAffine2D.rotate\u001b[0;34m(self, theta)\u001b[0m\n\u001b[1;32m   2004\u001b[0m a \u001b[38;5;241m=\u001b[39m math\u001b[38;5;241m.\u001b[39mcos(theta)\n\u001b[1;32m   2005\u001b[0m b \u001b[38;5;241m=\u001b[39m math\u001b[38;5;241m.\u001b[39msin(theta)\n\u001b[0;32m-> 2006\u001b[0m rotate_mtx \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   2007\u001b[0m \u001b[43m                      \u001b[49m\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2008\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_mtx \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mdot(rotate_mtx, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_mtx)\n\u001b[1;32m   2009\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minvalidate()\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error in callback <function flush_figures at 0x7fede1996f70> (for post_execute):\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib_inline/backend_inline.py:121\u001b[0m, in \u001b[0;36mflush_figures\u001b[0;34m()\u001b[0m\n\u001b[1;32m    118\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m InlineBackend\u001b[38;5;241m.\u001b[39minstance()\u001b[38;5;241m.\u001b[39mclose_figures:\n\u001b[1;32m    119\u001b[0m     \u001b[38;5;66;03m# ignore the tracking, just draw and close all figures\u001b[39;00m\n\u001b[1;32m    120\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 121\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mshow\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m    122\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    123\u001b[0m         \u001b[38;5;66;03m# safely show traceback if in IPython, else raise\u001b[39;00m\n\u001b[1;32m    124\u001b[0m         ip \u001b[38;5;241m=\u001b[39m get_ipython()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib_inline/backend_inline.py:41\u001b[0m, in \u001b[0;36mshow\u001b[0;34m(close, block)\u001b[0m\n\u001b[1;32m     39\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m     40\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m figure_manager \u001b[38;5;129;01min\u001b[39;00m Gcf\u001b[38;5;241m.\u001b[39mget_all_fig_managers():\n\u001b[0;32m---> 41\u001b[0m         \u001b[43mdisplay\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     42\u001b[0m \u001b[43m            \u001b[49m\u001b[43mfigure_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanvas\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     43\u001b[0m \u001b[43m            \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_fetch_figure_metadata\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfigure_manager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanvas\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     44\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     45\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     46\u001b[0m     show\u001b[38;5;241m.\u001b[39m_to_draw \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/display_functions.py:298\u001b[0m, in \u001b[0;36mdisplay\u001b[0;34m(include, exclude, metadata, transient, display_id, raw, clear, *objs, **kwargs)\u001b[0m\n\u001b[1;32m    296\u001b[0m     publish_display_data(data\u001b[38;5;241m=\u001b[39mobj, metadata\u001b[38;5;241m=\u001b[39mmetadata, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m    297\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 298\u001b[0m     format_dict, md_dict \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mformat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minclude\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexclude\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexclude\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    299\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m format_dict:\n\u001b[1;32m    300\u001b[0m         \u001b[38;5;66;03m# nothing to display (e.g. _ipython_display_ took over)\u001b[39;00m\n\u001b[1;32m    301\u001b[0m         \u001b[38;5;28;01mcontinue\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/formatters.py:178\u001b[0m, in \u001b[0;36mDisplayFormatter.format\u001b[0;34m(self, obj, include, exclude)\u001b[0m\n\u001b[1;32m    176\u001b[0m md \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    177\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 178\u001b[0m     data \u001b[38;5;241m=\u001b[39m \u001b[43mformatter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    179\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n\u001b[1;32m    180\u001b[0m     \u001b[38;5;66;03m# FIXME: log the exception\u001b[39;00m\n\u001b[1;32m    181\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/decorator.py:232\u001b[0m, in \u001b[0;36mdecorate.<locals>.fun\u001b[0;34m(*args, **kw)\u001b[0m\n\u001b[1;32m    230\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kwsyntax:\n\u001b[1;32m    231\u001b[0m     args, kw \u001b[38;5;241m=\u001b[39m fix(args, kw, sig)\n\u001b[0;32m--> 232\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcaller\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mextras\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/formatters.py:222\u001b[0m, in \u001b[0;36mcatch_format_error\u001b[0;34m(method, self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    220\u001b[0m \u001b[38;5;124;03m\"\"\"show traceback on failed format call\"\"\"\u001b[39;00m\n\u001b[1;32m    221\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 222\u001b[0m     r \u001b[38;5;241m=\u001b[39m \u001b[43mmethod\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    223\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m:\n\u001b[1;32m    224\u001b[0m     \u001b[38;5;66;03m# don't warn on NotImplementedErrors\u001b[39;00m\n\u001b[1;32m    225\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_return(\u001b[38;5;28;01mNone\u001b[39;00m, args[\u001b[38;5;241m0\u001b[39m])\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/formatters.py:339\u001b[0m, in \u001b[0;36mBaseFormatter.__call__\u001b[0;34m(self, obj)\u001b[0m\n\u001b[1;32m    337\u001b[0m     \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[1;32m    338\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 339\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mprinter\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    340\u001b[0m \u001b[38;5;66;03m# Finally look for special method names\u001b[39;00m\n\u001b[1;32m    341\u001b[0m method \u001b[38;5;241m=\u001b[39m get_real_method(obj, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprint_method)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/IPython/core/pylabtools.py:151\u001b[0m, in \u001b[0;36mprint_figure\u001b[0;34m(fig, fmt, bbox_inches, base64, **kwargs)\u001b[0m\n\u001b[1;32m    148\u001b[0m     \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbackend_bases\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FigureCanvasBase\n\u001b[1;32m    149\u001b[0m     FigureCanvasBase(fig)\n\u001b[0;32m--> 151\u001b[0m \u001b[43mfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanvas\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprint_figure\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbytes_io\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    152\u001b[0m data \u001b[38;5;241m=\u001b[39m bytes_io\u001b[38;5;241m.\u001b[39mgetvalue()\n\u001b[1;32m    153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m fmt \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msvg\u001b[39m\u001b[38;5;124m'\u001b[39m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/backend_bases.py:2295\u001b[0m, in \u001b[0;36mFigureCanvasBase.print_figure\u001b[0;34m(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)\u001b[0m\n\u001b[1;32m   2289\u001b[0m     renderer \u001b[38;5;241m=\u001b[39m _get_renderer(\n\u001b[1;32m   2290\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfigure,\n\u001b[1;32m   2291\u001b[0m         functools\u001b[38;5;241m.\u001b[39mpartial(\n\u001b[1;32m   2292\u001b[0m             print_method, orientation\u001b[38;5;241m=\u001b[39morientation)\n\u001b[1;32m   2293\u001b[0m     )\n\u001b[1;32m   2294\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(renderer, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_draw_disabled\u001b[39m\u001b[38;5;124m\"\u001b[39m, nullcontext)():\n\u001b[0;32m-> 2295\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2297\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m bbox_inches:\n\u001b[1;32m   2298\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m bbox_inches \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtight\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:73\u001b[0m, in \u001b[0;36m_finalize_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer, *args, **kwargs)\u001b[0m\n\u001b[1;32m     71\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(draw)\n\u001b[1;32m     72\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdraw_wrapper\u001b[39m(artist, renderer, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m---> 73\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     74\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m renderer\u001b[38;5;241m.\u001b[39m_rasterizing:\n\u001b[1;32m     75\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstop_rasterizing()\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:50\u001b[0m, in \u001b[0;36mallow_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer)\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m     48\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstart_filter()\n\u001b[0;32m---> 50\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/figure.py:2810\u001b[0m, in \u001b[0;36mFigure.draw\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m   2807\u001b[0m         \u001b[38;5;66;03m# ValueError can occur when resizing a window.\u001b[39;00m\n\u001b[1;32m   2809\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpatch\u001b[38;5;241m.\u001b[39mdraw(renderer)\n\u001b[0;32m-> 2810\u001b[0m \u001b[43mmimage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_draw_list_compositing_images\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   2811\u001b[0m \u001b[43m    \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43martists\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msuppressComposite\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2813\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m sfig \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msubfigs:\n\u001b[1;32m   2814\u001b[0m     sfig\u001b[38;5;241m.\u001b[39mdraw(renderer)\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/image.py:132\u001b[0m, in \u001b[0;36m_draw_list_compositing_images\u001b[0;34m(renderer, parent, artists, suppress_composite)\u001b[0m\n\u001b[1;32m    130\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m not_composite \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m has_images:\n\u001b[1;32m    131\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m a \u001b[38;5;129;01min\u001b[39;00m artists:\n\u001b[0;32m--> 132\u001b[0m         \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    133\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    134\u001b[0m     \u001b[38;5;66;03m# Composite any adjacent images together\u001b[39;00m\n\u001b[1;32m    135\u001b[0m     image_group \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:50\u001b[0m, in \u001b[0;36mallow_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer)\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m     48\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstart_filter()\n\u001b[0;32m---> 50\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/axes/_base.py:3082\u001b[0m, in \u001b[0;36m_AxesBase.draw\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m   3079\u001b[0m         a\u001b[38;5;241m.\u001b[39mdraw(renderer)\n\u001b[1;32m   3080\u001b[0m     renderer\u001b[38;5;241m.\u001b[39mstop_rasterizing()\n\u001b[0;32m-> 3082\u001b[0m \u001b[43mmimage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_draw_list_compositing_images\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   3083\u001b[0m \u001b[43m    \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43martists\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfigure\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msuppressComposite\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   3085\u001b[0m renderer\u001b[38;5;241m.\u001b[39mclose_group(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxes\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m   3086\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstale \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/image.py:132\u001b[0m, in \u001b[0;36m_draw_list_compositing_images\u001b[0;34m(renderer, parent, artists, suppress_composite)\u001b[0m\n\u001b[1;32m    130\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m not_composite \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m has_images:\n\u001b[1;32m    131\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m a \u001b[38;5;129;01min\u001b[39;00m artists:\n\u001b[0;32m--> 132\u001b[0m         \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    133\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    134\u001b[0m     \u001b[38;5;66;03m# Composite any adjacent images together\u001b[39;00m\n\u001b[1;32m    135\u001b[0m     image_group \u001b[38;5;241m=\u001b[39m []\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/artist.py:50\u001b[0m, in \u001b[0;36mallow_rasterization.<locals>.draw_wrapper\u001b[0;34m(artist, renderer)\u001b[0m\n\u001b[1;32m     47\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m     48\u001b[0m         renderer\u001b[38;5;241m.\u001b[39mstart_filter()\n\u001b[0;32m---> 50\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mdraw\u001b[49m\u001b[43m(\u001b[49m\u001b[43martist\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m artist\u001b[38;5;241m.\u001b[39mget_agg_filter() \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/text.py:685\u001b[0m, in \u001b[0;36mText.draw\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m    682\u001b[0m renderer\u001b[38;5;241m.\u001b[39mopen_group(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtext\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_gid())\n\u001b[1;32m    684\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cm_set(text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_wrapped_text()):\n\u001b[0;32m--> 685\u001b[0m     bbox, info, descent \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_layout\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrenderer\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    686\u001b[0m     trans \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mget_transform()\n\u001b[1;32m    688\u001b[0m     \u001b[38;5;66;03m# don't use self.get_position here, which refers to text\u001b[39;00m\n\u001b[1;32m    689\u001b[0m     \u001b[38;5;66;03m# position in Text:\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/repids-22.04/lib/python3.8/site-packages/matplotlib/text.py:441\u001b[0m, in \u001b[0;36mText._get_layout\u001b[0;34m(self, renderer)\u001b[0m\n\u001b[1;32m    438\u001b[0m bbox \u001b[38;5;241m=\u001b[39m Bbox\u001b[38;5;241m.\u001b[39mfrom_bounds(xmin, ymin, width, height)\n\u001b[1;32m    440\u001b[0m \u001b[38;5;66;03m# now rotate the positions around the first (x, y) position\u001b[39;00m\n\u001b[0;32m--> 441\u001b[0m xys \u001b[38;5;241m=\u001b[39m \u001b[43mM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtransform\u001b[49m\u001b[43m(\u001b[49m\u001b[43moffset_layout\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43moffsetx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moffsety\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    443\u001b[0m ret \u001b[38;5;241m=\u001b[39m bbox, \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28mzip\u001b[39m(lines, \u001b[38;5;28mzip\u001b[39m(ws, hs), \u001b[38;5;241m*\u001b[39mxys\u001b[38;5;241m.\u001b[39mT)), descent\n\u001b[1;32m    444\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cached[key] \u001b[38;5;241m=\u001b[39m ret\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "cm = confusion_matrix(y_test, y_pred, labels=y)\n",
    "disp = ConfusionMatrixDisplay(confusion_matrix=cm,\n",
    "                              display_labels=target_names)\n",
    "disp.plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8c108b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5146c9eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 20 eigenfaces from 966 faces\n",
      "done in 0.736s\n"
     ]
    }
   ],
   "source": [
    "n_components = 20\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bfd58022",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.025s\n"
     ]
    }
   ],
   "source": [
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5f5dab25",
   "metadata": {},
   "outputs": [],
   "source": [
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sqDistances.argsort()[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c166e73a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.00      0.00      0.00        13\n",
      "     Colin Powell       0.16      0.17      0.17        60\n",
      "  Donald Rumsfeld       0.06      0.07      0.07        27\n",
      "    George W Bush       0.47      0.43      0.45       146\n",
      "Gerhard Schroeder       0.14      0.08      0.10        25\n",
      "      Hugo Chavez       0.06      0.07      0.06        15\n",
      "       Tony Blair       0.22      0.25      0.23        36\n",
      "\n",
      "         accuracy                           0.27       322\n",
      "        macro avg       0.16      0.15      0.15       322\n",
      "     weighted avg       0.29      0.27      0.28       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(classification_report(y_test, y_pred, target_names=target_names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "00ec07ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 30 eigenfaces from 966 faces\n",
      "done in 0.761s\n",
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.021s\n",
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.05      0.08      0.06        13\n",
      "     Colin Powell       0.19      0.15      0.17        60\n",
      "  Donald Rumsfeld       0.11      0.15      0.12        27\n",
      "    George W Bush       0.46      0.40      0.42       146\n",
      "Gerhard Schroeder       0.06      0.08      0.07        25\n",
      "      Hugo Chavez       0.00      0.00      0.00        15\n",
      "       Tony Blair       0.02      0.03      0.03        36\n",
      "\n",
      "         accuracy                           0.23       322\n",
      "        macro avg       0.13      0.13      0.12       322\n",
      "     weighted avg       0.26      0.23      0.25       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "\n",
    "n_components = 30\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))\n",
    "\n",
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sqDistances.argsort()[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown\n",
    "\n",
    "print(classification_report(y_test, y_pred, target_names=target_names))        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a4ba604c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 40 eigenfaces from 966 faces\n",
      "done in 0.776s\n",
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.029s\n",
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.05      0.08      0.06        13\n",
      "     Colin Powell       0.15      0.13      0.14        60\n",
      "  Donald Rumsfeld       0.07      0.07      0.07        27\n",
      "    George W Bush       0.48      0.46      0.47       146\n",
      "Gerhard Schroeder       0.06      0.04      0.05        25\n",
      "      Hugo Chavez       0.07      0.07      0.07        15\n",
      "       Tony Blair       0.09      0.11      0.10        36\n",
      "\n",
      "         accuracy                           0.26       322\n",
      "        macro avg       0.14      0.14      0.14       322\n",
      "     weighted avg       0.27      0.26      0.26       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "\n",
    "n_components = 40\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))\n",
    "\n",
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sqDistances.argsort()[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown\n",
    "\n",
    "print(classification_report(y_test, y_pred, target_names=target_names))        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6b368ae3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 50 eigenfaces from 966 faces\n",
      "done in 0.770s\n",
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.028s\n",
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.00      0.00      0.00        13\n",
      "     Colin Powell       0.22      0.23      0.23        60\n",
      "  Donald Rumsfeld       0.13      0.15      0.14        27\n",
      "    George W Bush       0.46      0.45      0.46       146\n",
      "Gerhard Schroeder       0.11      0.08      0.09        25\n",
      "      Hugo Chavez       0.00      0.00      0.00        15\n",
      "       Tony Blair       0.12      0.11      0.12        36\n",
      "\n",
      "         accuracy                           0.28       322\n",
      "        macro avg       0.15      0.15      0.15       322\n",
      "     weighted avg       0.28      0.28      0.28       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "\n",
    "n_components = 50\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))\n",
    "\n",
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sqDistances.argsort()[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown\n",
    "\n",
    "print(classification_report(y_test, y_pred, target_names=target_names))        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "36512b7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 60 eigenfaces from 966 faces\n",
      "done in 0.826s\n",
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.018s\n",
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.00      0.00      0.00        13\n",
      "     Colin Powell       0.24      0.23      0.24        60\n",
      "  Donald Rumsfeld       0.03      0.04      0.03        27\n",
      "    George W Bush       0.46      0.42      0.44       146\n",
      "Gerhard Schroeder       0.00      0.00      0.00        25\n",
      "      Hugo Chavez       0.04      0.07      0.05        15\n",
      "       Tony Blair       0.12      0.11      0.12        36\n",
      "\n",
      "         accuracy                           0.25       322\n",
      "        macro avg       0.13      0.12      0.13       322\n",
      "     weighted avg       0.27      0.25      0.26       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "\n",
    "n_components = 60\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))\n",
    "\n",
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sqDistances.argsort()[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown\n",
    "\n",
    "print(classification_report(y_test, y_pred, target_names=target_names))        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b62fa684",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting the top 100 eigenfaces from 966 faces\n",
      "done in 0.739s\n",
      "Projecting the input data on the eigenfaces orthonormal basis\n",
      "done in 0.041s\n",
      "                   precision    recall  f1-score   support\n",
      "\n",
      "     Ariel Sharon       0.00      0.00      0.00        13\n",
      "     Colin Powell       0.17      0.15      0.16        60\n",
      "  Donald Rumsfeld       0.05      0.07      0.06        27\n",
      "    George W Bush       0.46      0.42      0.44       146\n",
      "Gerhard Schroeder       0.14      0.12      0.13        25\n",
      "      Hugo Chavez       0.06      0.07      0.06        15\n",
      "       Tony Blair       0.08      0.08      0.08        36\n",
      "\n",
      "         accuracy                           0.25       322\n",
      "        macro avg       0.14      0.13      0.13       322\n",
      "     weighted avg       0.27      0.25      0.26       322\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n",
    "\n",
    "# Standardize features by removing the mean and scaling to unit variance.\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "\n",
    "n_components = 100\n",
    "print('Extracting the top %d eigenfaces from %d faces' % (n_components, X_train.shape[0]))\n",
    "t0 = time()\n",
    "pca = PCA(n_components=n_components, svd_solver='full', whiten=True)\n",
    "pca.fit(X_train)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "eigenfaces = pca.components_.reshape((n_components, h, w))\n",
    "\n",
    "print('Projecting the input data on the eigenfaces orthonormal basis')\n",
    "t0 = time()\n",
    "X_train_pca = pca.transform(X_train)\n",
    "# 把测试集的人脸投影到10维空间\n",
    "X_test_pca = pca.transform(X_test)\n",
    "print('done in %0.3fs' % (time() - t0))\n",
    "\n",
    "Threshold = 100000\n",
    "# 得到测试脸在特征向量下的数据\n",
    "test_new = np.array(np.dot(X_test_pca, pca.components_[:, 0:n_components]))\n",
    "train_new = np.array(np.dot(X_train_pca,pca.components_[:, 0:n_components]))\n",
    "y_pred = [-1 for i in range(len(test_new))] # 测试集的预测值\n",
    "for i in range(len(test_new)):\n",
    "    testFace = test_new[i, :]\n",
    "    diffMat = train_new - np.tile(testFace, (len(train_new), 1)) # 计算训练数据与测试数据的距离\n",
    "    sqDiffMat = diffMat**2\n",
    "    sqDistances = sqDiffMat.sum(axis=1)\n",
    "    sortedDistIndicies = sqDistances.argsort()  # 对向量从小到大排序，使用的是索引值,得到一个向量\n",
    "    indexMin = sortedDistIndicies[0]  # 距离最近的索引\n",
    "    if sqDistances.argsort()[0] < Threshold:\n",
    "        y_pred[i] = y[indexMin]\n",
    "    else:\n",
    "        y_pred[i] = -1 # -1表示unkown\n",
    "\n",
    "print(classification_report(y_test, y_pred, target_names=target_names))        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8fec28e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
